﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Models.Price;
using Sahit.iSoilHotel.Portal.ServiceProxy.TavernPriceServiceReference;
using AutoMapper;
using Sahit.iSoilHotel.Infrastructure.Web.Caching;
using Sahit.iSoilHotel.Infrastructure.Web.Events;
using Sahit.iSoilHotel.Portal.ServiceProxy.Common;

namespace Sahit.iSoilHotel.Portal.ServiceProxy.Tavern.Proxys.Price
{
    public class TavernPriceServiceProxy : ITavernPriceServiceProxy
    {
        private TavernPriceServiceClient tavernPriceService;
        private readonly ICacheManager cacheManager;
        private readonly IEventPublisher eventPublisher;

        public TavernPriceServiceProxy(ICacheManager cacheManager,
            IEventPublisher eventPublisher)
        {
            this.cacheManager = cacheManager;
            this.eventPublisher = eventPublisher;
        }

        public List<PriceCodeModel> GetAllPriceCodes()
        {
            return cacheManager.Get(CacheKeysContainer.TAVERN_PRICECODE_ALL, () =>
            {
                using (tavernPriceService = new TavernPriceServiceClient())
                {
                    PriceCodeDTO[] priceCodes = tavernPriceService.GetAllPriceCodes();
                    return Mapper.Map<List<PriceCodeModel>>(priceCodes);
                }
            });
        }


        public PriceModel GetPrice(Guid roomTypeID)
        {
            return cacheManager.Get(String.Format(CacheKeysContainer.TAVERN_PRICE_ROOMTYPE_PATTERN,roomTypeID), () =>
            {
                using (tavernPriceService = new TavernPriceServiceClient())
                {
                    PriceDTO price = tavernPriceService.GetPrice(roomTypeID);
                    return Mapper.Map<PriceModel>(price);
                }
            });
        }
    }
}
